﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using blackhouse.NemoBlog.Business.View;
using blackhouse.WebNemo.Business.Users;
using blackhouse.NemoBlog.Business.Model;
using System.Configuration;

namespace blackhouse.NemoBlog.Business.Presenter {
    public class PostPresenter {

        #region Fields

        private IPostView view;
        private INemoUser user = blackhouse.WebNemo.Business.Factory.Instance().GetCurrentUser();
        private bool isPublisher;
        private IPostModel model;

        #endregion

        #region Constructors

        public PostPresenter(IPostView view, IPostModel model) {
            this.view = view;
            blackhouse.NemoBlog.Business.Install.NemoBlogInstaller.CheckInstall();
            this.isPublisher = this.user.HasSufficientPermissions(false);
            this.model = model;
            if (this.model == null)
                this.model = new StandardPostModel(this.view.PostId);
        }

        #endregion

        #region Methods

        public void Load() {
            if (this.model.Post == null || !this.model.Post.Exists) {
                this.view.Visible = false;
                return;
            }
            this.view.SetValidUrl(this.model.Post.PostUrl);
            this.view.PostTitle = this.model.Post.Title;
            this.view.ShortTitle = this.model.Post.ShortTitle;
            this.view.ShortDescription = this.model.Post.ShortDescription;
            this.view.PostContent = this.model.Post.Content;
            if (model.Post.IsPublished) {
                this.view.PublishedDateText = this.model.Post.PublishDate.ToString("D");
                this.view.IsoPublishedDateText = this.model.Post.PublishDate.ToString("s");
            }
            else
                this.view.PublishedDateText = "**** Nie opublikowano ***";
            this.view.Publisher = blackhouse.WebNemo.Business.Factory.Instance().GetUser(this.model.Post.CreatedBy).Name;
            if (this.model.Settings.UseTagSystem)
                this.view.PopulateTags(this.model.Post.Tags);

            if (this.isPublisher) {
                this.view.SetRemoveLink(this.model.Post.PostId);
                this.view.SetEditLink(this.model.Post);
                if (!this.model.Post.IsPublished)
                    this.view.SetPublishLink(this.model.Post.PostId);
            }

            NemoBlogPost[] similarPosts = this.model.GetSimilarPosts();
            if (similarPosts.Length == 0)
                this.view.SimilarPostsVisible = false;
            else
                this.view.PopulateSimilarPosts(similarPosts);
        }

        public void Remove() {
            this.model.Post.Remove();
            this.view.ShowPostLists(this.model.Post.ModuleId);
        }

        public void Publish() {
            this.model.Post.IsPublished = true;
            this.model.Post.PublishDate = DateTime.Now;
            this.model.Post.Save();
            this.view.Refresh();
        }

        public void AttachEvents() {
            this.view.Remove += view_Remove;
            this.view.Publish += view_Publish;
        }

        void view_Publish(object sender, EventArgs e) {
            this.Publish();
        }

        void view_Remove(object sender, EventArgs e) {
            this.Remove();
        }

        #endregion

    }
}
